<?php
/**
 * Created by zw.
 * User: Administrator
 * Date: 14-8-18
 * Time: 下午3:40
 */

/**
 *取得文件后缀
 */
function extend($file_name){
    $extend = pathinfo($file_name);
    $extend = strtolower($extend["extension"]);
    return $extend;
}

/**
 *自动获取模板文件
 */
function getTpl($type='',$path='templates'){
    $theme = C('SYS_DEFAULT_THEME');

    $path = APP_PATH.'Home/View/'.$theme.'/';

    //die($path);
    $dir = opendir($path);

    $arr = array('index_','list_','content_','cont_');
    if(!in_array($type.'_',$arr)){
        return;
    }else {

        while(($file = readdir($dir)) != false){
            if(!is_dir($file)&&is_file($path.$file)){
                if(extend($path.$file)=='html'){
                    $arr =  explode('_',$file);
                    if($arr[0]==$type)
                        $rs[$path.$file] = $file;
                    else continue;
                }
            }
        }
        if(empty($rs)) $rs =  array();
        return $rs;

    }
}

//启用状态
function status($d){
    if($d==1){
        return "启用";
    }else{
        return "禁用";
    }
}
//显示状态
function display($d){
    if($d==1){
        return "显示";
    }else{
        return "隐藏";
    }
}
//审核状态
function checkStatus($d){
    switch($d){
        case 0 : $rs = "未审核"; break;
        case 1 : $rs = "已审核"; break;
        case 3 : $rs = "测试";break;
        case -1 : $rs = "回收站"; break;
        default:$rs = "未知"; break;
    }
    return $rs;
}

/**
 * 获得字段创建信息
 *
 * @access    public
 * @param     string  $dtype  字段类型
 * @param     string  $fieldname  字段名称
 * @param     string  $dfvalue  默认值
 * @param     string  $mxlen  最大字符长度
 * @return    array
 */
function GetFieldMake($dtype, $fieldname, $dfvalue, $mxlen)
{
	$fields = array();
	if($dtype == "int" || $dtype == "datetime")
	{
		if($dfvalue == "" || preg_match("#[^0-9-]#", $dfvalue))
		{
			$dfvalue = 0;
		}
		$fields[0] = " `$fieldname` int(11) NOT NULL default '$dfvalue';";
		$fields[1] = "int(11)";
	}
	else if($dtype == "stepselect")
	{
		if($dfvalue == "" || preg_match("#[^0-9\.-]#", $dfvalue))
		{
			$dfvalue = 0;
		}
		$fields[0] = " `$fieldname` char(20) NOT NULL default '$dfvalue';";
		$fields[1] = "char(20)";
	}
	else if($dtype == "float")
	{
		if($dfvalue == "" || preg_match("#[^0-9\.-]#", $dfvalue))
		{
			$dfvalue = 0;
		}
		$fields[0] = " `$fieldname` float NOT NULL default '$dfvalue';";
		$fields[1] = "float";
	}
	else if($dtype == "img" || $dtype == "media" || $dtype == "addon" || $dtype == "imgfile")
	{
		if(empty($dfvalue)) $dfvalue = '';
		if($mxlen=="") $mxlen = 200;
		if($mxlen > 255) $mxlen = 100;

		$fields[0] = " `$fieldname` varchar($mxlen) NOT NULL default '$dfvalue';";
		$fields[1] = "varchar($mxlen)";
	}
	else if($dtype == "multitext" || $dtype == "htmltext")
	{
		$fields[0] = " `$fieldname` mediumtext;";
		$fields[1] = "mediumtext";
	}
	else if($dtype=="textdata")
	{
		if(empty($dfvalue)) $dfvalue = '';

		$fields[0] = " `$fieldname` varchar(100) NOT NULL default '';";
		$fields[1] = "varchar(100)";
	}
	else if($dtype=="textchar" || $dtype=="hidden")
	{
		if(empty($dfvalue)) $dfvalue = '';

		$fields[0] = " `$fieldname` char(100) NOT NULL default '$dfvalue';";
		$fields[1] = "char(100)";
	}
	else if($dtype=="checkbox")
	{
        if(preg_match("#^__.*__$#i", $dfvalue)){
            $fields[0] = " `$fieldname` char(255) NOT NULL default '';";
            $fields[1] = "char(255)";
        }else{
            $dfvalue = str_replace(',',"','",$dfvalue);
            $dfvalue = "'".$dfvalue."'";
            $fields[0] = " `$fieldname` SET($dfvalue) NULL;";
            $fields[1] = "SET($dfvalue)";
        }
	}
	else if($dtype=="select" || $dtype=="radio")
	{
        if(preg_match("#^__.*__$#i", $dfvalue)){
            $fields[0] = " `$fieldname` char(100) NOT NULL default '';";
            $fields[1] = "char(100)";
        }else{
            $dfvalue = str_replace(',', "','", $dfvalue);
            $dfvalue = "'".$dfvalue."'";
            $fields[0] = " `$fieldname` enum($dfvalue) NULL;";
            $fields[1] = "enum($dfvalue)";
        }
	}
	else
	{
		if(empty($dfvalue))
		{
			$dfvalue = '';
		}
		if(empty($mxlen))
		{
			$mxlen = 100;
		}
		if($mxlen > 255)
		{
			$mxlen = 250;
		}
		$fields[0] = " `$fieldname` varchar($mxlen) NOT NULL default '$dfvalue';";
		$fields[1] = "varchar($mxlen)";
	}
	return $fields;
}


/**
 *获取模型里面自动创建的字段类型,
 * 根据不同类型对应不同的表单形式
 */
function AutoField($fields){
    foreach($fields as $v){
        switch($v['type']){
            case 'text':{
               $form='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                         <input type="text" name='.$v['name'].' class="form-control" />
                         </div>
                         </div>';
                                break;
        }
            case 'multitext' :{
               $form.='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                         <textarea name='.$v['name'].' class="form-control" rows="3"></textarea>
                         </div>
                         </div>';
                                  break;
            }
            case 'htmltext':{
                $form.='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                         <textarea id="editor_id" name='.$v['name'].' style="width:700px;height:360px;"></textarea>
                         </div>
                         </div>';
                break;
            }
            case 'datetime':{
                $form.='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                         <input id="datepicker" type="text" name='.$v['name'].' />
                         </div>
                         </div>';
                                break;
            }
            case 'img':{
                $form.='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                         <label>
                            <input type="text" id="url1" name='.$v['name'].' value="" class="form-control" />
                         </label>
                         <label>
                            <input type="button" id="image1" value="浏览" class="btn" />
                         </label>
                         <p><img src="/mycms/Public/images/default.gif" id="imgurl" class="img-rounded" style="max-width: 300px;max-height: 100px;" /></p>
                      </div></div>';
                break;
            }
            case 'media':{
                $form.='<div class="form-group">
                         <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                         <div class="col-sm-4">
                           <label>
                            <input type="text" id="url2" value="" name='.$v['name'].' class="form-control" />
                         </label>
                         <label>
                            <input type="button" id="media" value="选择文件" class="btn" />
                         </label>
                         </div>
                         </div>';
                             break;
            }
            case 'select':{
                $arr=explode(',',$v['default']);
                $form.='<div class="form-group">
                        <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                        <div class="col-sm-4">
                            <select class="form-control" name='.$v['name'].'>
                                <option value="0">--请选择--</option>';
              foreach($arr as $rs){
                    $form.='<option value='.$rs.'>'.$rs.'</option>';
                }
                $form.='</select></div></div>';
                              break;
            }
            case 'radio':{
                $arr=explode(',',$v['default']);
                $form.='<div class="form-group">
                        <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                        <div class="col-sm-4">';
                foreach($arr as $rs){
                    $form.='<label class="radio-inline">
                            <input type="radio" name='.$v['name'].' id="status" value='.$rs.'>'.$rs.'
                            </label>';
                }
                $form.='</div></div>';
                             break;
            }
            case 'checkbox':{
                $arr=explode(',',$v['default']);
                $form.='<div class="form-group">
                        <label class="col-sm-2 control-label">'.$v['intro'].'</label>
                        <div class="col-sm-4">';
                foreach($arr as $rs){
                    $form.='<label class="checkbox-inline">
                            <input type="checkbox" name='.$v['name'].' id="status" value='.$rs.'>'.$rs.'
                            </label>';
                }
                $form.='</div></div>';
                break;
            }
        }
    }
return $form;
}

?>